package com.example.shushiyunsst.fragment.main.home.child.miner.sub

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner.bean.PlantTreeBean
import com.example.shushiyunsst.fragment.main.home.child.miner.bean.StartTransferBuyingSubBean
import com.example.shushiyunsst.util.GridItemDecoration
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 栽种星树
 * */
class PlantTreeFragment :SimpleListFragment<PlantTreeBean>() {

    companion object{
        fun newInstance(): PlantTreeFragment {
            val args = Bundle()
            val fragment = PlantTreeFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isEnableLoadMore=false
//        isAddDividerItemDecoration=false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.drawable.bg_theme)

        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)

        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.plant_tree_title)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        val  size = PixUtils.dp2px(10)
        mRecyclerView.setPadding(size,size,size,0)
        mRecyclerView.clipToPadding=false

        mAdapter.addChildClickViewIds(R.id.contractTypeTv)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (position==0){
                start(PlantResultsFragment.newInstance())
            }
        }
    }

    override fun getItemLayout()= R.layout.plant_tree_fragment

    override fun getLayoutManager(): RecyclerView.LayoutManager {
        return GridLayoutManager(_mActivity,2)
    }
    override fun addItemDecoration(): RecyclerView.ItemDecoration {
        val  size = PixUtils.dp2px(10)
        return GridItemDecoration(size,size)
    }
    override fun convertItem(helper: JssBaseViewHolder?, item: PlantTreeBean?) {
        val flags=(helper?.layoutPosition?:0)==0
        helper
            ?.setText(R.id.platformTypeTv,if (flags)"已种栽" else "种栽")
            ?.setText(R.id.contractTypeTv,if (flags)"查看" else "种栽")
            ?.setViewSelect(R.id.contractTypeTv,flags)
            ?.setViewSelect(R.id.platformTypeTv,flags)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(PlantTreeBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.temp_fack_data)
        parseDate(jsonFromRaw)
    }
}